package com.xebest.web.controller.storehouse;

import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.XeVenExcelUtil;
import com.xebest.web.controller.export.XeVenInventoryExportDTO;
import com.xebest.web.controller.export.XeVenStoreCheckExportDTO;
import com.xebest.web.controller.export.XeVenStoreExportDTO;
import com.xebest.web.form.XeVenStoreCheckForm;
import com.xebest.web.form.XeVenStoreInfoForm;
import com.xebest.web.form.XeVenStoreInvForm;
import com.xebest.web.service.XeVenStoreCenterExportService;
import com.xebest.web.view.ErpUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 仓储中心导出
 *
 * @author 赵盼飞
 */
@RestController
@RequestMapping("storeCenterExport")
public class XeVenStoreCenterExportController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenStoreCenterExportController.class);//日志记录类

    @Autowired
    private XeVenStoreCenterExportService storeCenterExportService;

    @Value("${xeven.erp.exportTempletDir}")
    private String exportTempletDir;//导出文件目录

    /**
     * 导出仓库列表
     *
     * @author 赵盼飞
     */
    @RequestMapping("storeList")
    public @ResponseBody
    BaseResponse queryStoreExportList(@ModelAttribute XeVenStoreInfoForm form, HttpServletResponse response) {

        try {
            String[] str = new String[]{"storeName", "storeStatus", "storeId", "tmsStatus", "storeType",
                    "recordInventoryFlag", "negativeFlag", "contactPerson", "storeTel", "storeArea", "storeAddress",
                    "coverAreaId"};
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("form", form);
            List<XeVenStoreExportDTO> storeExportList = storeCenterExportService.queryStoreExportList(paramMap);
//			if(null != storeExportList && storeExportList.size()>0){
            //3.导出
            boolean rs = XeVenExcelUtil.exportExcelService(storeExportList, new XeVenStoreExportDTO(), form.getFieldArr(), exportTempletDir, "STORELIST-", response);
            if (rs) {
                return sendAjaxOK();
            } else {
                return sendAjaxError();
            }
//			}else{
//				return sendAjaxOK("导出数据为空！");
//			}
        } catch (Exception e) {
            log.error("导出仓库列表接口异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 导出库存监控列表
     *
     * @author 赵盼飞
     */
    @RequestMapping("storeInventoryList")
    public @ResponseBody
    BaseResponse queryStoreInventoryExportList(@ModelAttribute XeVenStoreInvForm form, HttpServletResponse response) {

        try {
            String[] str = new String[]{"proNum", "proName", "proCategoryName", "proPricingType", "proUnit",
                    "proSepcValue", "averagePrice", "storeName", "actualInv", "invAmount", "usableInv", "lockedInv",
                    "transitInv", "invMax", "invMin"};
            form.setFieldArr(str);

            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("form", form);
            List<XeVenInventoryExportDTO> storeInventoryExportList = storeCenterExportService.queryStoreInventoryExportList(paramMap);
//			if(null != storeInventoryExportList && storeInventoryExportList.size()>0){
            //3.导出
            boolean rs = XeVenExcelUtil.exportExcelService(storeInventoryExportList, new XeVenInventoryExportDTO(), form.getFieldArr(), exportTempletDir, "INVENTORYLIST-", response);
            if (rs) {
                return sendAjaxOK();
            } else {
                return sendAjaxError();
            }
//			}else{
//				return sendAjaxOK("导出数据为空！");
//			}
        } catch (Exception e) {
            log.error("导出库存监控列表接口异常：{}", e);
            return sendAjaxError();
        }
    }

    /**
     * 导出盘点单列表
     *
     * @author 赵盼飞
     */
    @RequestMapping("storeCheckList")
    public @ResponseBody
    BaseResponse queryStoreCheckExportList(@ModelAttribute XeVenStoreCheckForm form, HttpServletResponse response) {
        try {

            String[] str = new String[]{"checkCode", "storeId", "storeName", "checkStockStatus", "createDate",
                    "durationDate", "finishDate", "createUser", "checkRemark"};
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("form", form);
            List<XeVenStoreCheckExportDTO> storeInventoryExportList = storeCenterExportService.queryStoreCheckExportList(paramMap);
            //3.导出
//			if(null != storeInventoryExportList && storeInventoryExportList.size()>0){
            boolean rs = XeVenExcelUtil.exportExcelService(storeInventoryExportList, new XeVenStoreCheckExportDTO(), form.getFieldArr(), exportTempletDir, "STORECHECKLIST-", response);
            if (rs) {
                return sendAjaxOK();
            } else {
                return sendAjaxError();
            }
//			}else{
//				return sendAjaxOK("导出数据为空！");
//			}
        } catch (Exception e) {
            log.error("导出盘点单列表接口异常：{}", e);
            return sendAjaxError();
        }
    }

    /**
     * 导出盘点单明细
     *
     * @author 赵盼飞
     */
    @RequestMapping("storeCheckDetailList")
    public @ResponseBody
    BaseResponse queryStoreCheckDetailExportList(@ModelAttribute XeVenStoreCheckForm form, HttpServletResponse response) {
        try {

            String[] str = new String[]{"checkCode", "storeId", "storeName", "checkStockStatus", "createDate",
                    "durationDate", "finishDate", "createUser", "checkRemark", "proNum", "proName", "proCategoryName",
                    "pricingType", "proUnit", "proSpecValue", "stockNum", "checkStockNum", "checkStockResult"};
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("form", form);
            List<XeVenStoreCheckExportDTO> storeInventoryExportList = storeCenterExportService.queryStoreCheckDetailExportList(paramMap);
            //3.导出
//			if(null != storeInventoryExportList && storeInventoryExportList.size()>0){
            boolean rs = XeVenExcelUtil.exportExcelService(storeInventoryExportList, new XeVenStoreCheckExportDTO(), form.getFieldArr(), exportTempletDir, "STORECHECKDETAILLIST-", response);
            if (rs) {
                return sendAjaxOK();
            } else {
                return sendAjaxError();
            }
//			}else{
//				Map<String,Object> rsMap = new HashMap<String, Object>();
//				rsMap.put("info", "导出数据为空！");
//				return sendAjaxOK(rsMap);
//			}
        } catch (Exception e) {
            log.error("导出盘点单明细接口异常：{}", e);
            return sendAjaxError();
        }
    }


}